package com.yunshang.shoppingmall.controller.User;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yunshang.shoppingmall.common.HttpRequest;
import com.yunshang.shoppingmall.common.LayUIUtil;
import com.yunshang.shoppingmall.common.layuiJson;
import com.yunshang.shoppingmall.entity.Config;
import com.yunshang.shoppingmall.entity.User;
import com.yunshang.shoppingmall.service.ConfigService;
import com.yunshang.shoppingmall.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class UserController {
    @Autowired
    ConfigService configService;
    @Autowired
    UserService userService;

    //会员列表页面
    @RequestMapping("/user/getUserList")
    public String getUserNoList() {
        return "user/UserList";
    }

    @ResponseBody
    @RequestMapping("/user/JsonUserList")
    public LayUIUtil JsonUserList(HttpServletRequest request){
        Config config=configService.selectByPrimaryKey(1);
        String page = request.getParameter("page");
        String limit = request.getParameter("limit");
        int pages = 0;
        int limits = 10;
        if(page!=null&&page!=""){pages=Integer.parseInt(page);}
        if(limit!=null&&limit!=""){limits=Integer.parseInt(limit);}
        PageHelper.startPage(pages, limits);
        List<User> userList=userService.selectUserList();
        PageInfo<User> pageInfo=new PageInfo<>(userList);
        int count =0;
        count=(int)pageInfo.getTotal();

        return  LayUIUtil.data(count,userList);
    }
 @ResponseBody
@RequestMapping("/user/seachUserListByUserIdOrUserName")
public LayUIUtil seachUserListByUserIdOrUserName(@RequestParam("userName") String userName,@RequestParam("userId") String userId){
        if(userName==null&&userName==""&&userId==null&&userId==""){return null;}
        if(userName==""){
            userName=null;
        }
        User user=new User();
        user.setUserName(userName);
        int userIds=0;
        if(userId!=null&&userId!=""){
            userIds=Integer.parseInt(userId);
        }
        user.setUserId(userIds);
       List<User> userList= userService.seachUserListByUserIdOrUserName(user);
       PageInfo<User> pageInfo=new PageInfo<>(userList);
       int count=0;
       count=(int)pageInfo.getTotal();
       return LayUIUtil.data(count,userList);
}

    //读取添加会员资料的模板
    @RequestMapping("/user/getUserAdd")
    public ModelAndView getUserAdd() {
        ModelAndView modelAndView = new ModelAndView("user/UserAdd");
        return modelAndView;
    }

    //添加会员资料
    @ResponseBody
    @RequestMapping(value = "/user/addUser",method = RequestMethod.POST)
    public Map<String, Object> addUser(HttpServletRequest request) {
        String userName=request.getParameter("userName");
        String phone=request.getParameter("phone");
        String cardId=request.getParameter("cardId");
        String recommend= request.getParameter("recommend");
        String sort=request.getParameter("sort");
        Map<String, Object> map = new HashMap<>();
        if(userName==null&&userName==""||phone==null&&phone==""){
            map.put("msg", "0");
            return map;
        }
        User user=new User();
        user.setUserName(userName);
        int sorts=0;
        if(sort!=null&&sort!=""){
            sorts=Integer.parseInt(sort);
        }
        user.setSort(sorts);
        user.setCardId(cardId);
        user.setPhone(phone);
       int recommends=0;
        if(recommend!=null&&recommend!=""){
            recommends=Integer.parseInt(recommend);
        }
        user.setRecommend(recommends);
        user.setRegisterTime(new Timestamp(System.currentTimeMillis()));
        int row=userService.insertSelective(user);
       if(row>0){
           map.put("msg","1");
       }else{
           map.put("msg","0");
       }
       return map;
    }

    //根据编号ID查询会员等级信息
    @RequestMapping("/user/getUserEdit")
    public ModelAndView getUserEdit(@RequestParam("userId") int userId) {
        Config config=configService.selectByPrimaryKey(1);
        ModelAndView modelAndView = new ModelAndView("user/UserEdit");
        User user=userService.selectByPrimaryKey(userId);
        modelAndView.addObject("user",user);

        return modelAndView;
    }

    //编辑会员资
    @ResponseBody
    @RequestMapping(value = "/user/editUser")
    public Map<String, Object> editUser(@RequestParam("userId") int userId,
                                          @RequestParam("userName") String userName,
                                          @RequestParam("phone") String phone,
                                          @RequestParam("cardId") String cardId,
                                          @RequestParam("recommend") int recommend,
                                          @RequestParam("sort") int sort) {
        Map<String, Object> map = new HashMap<>();
        User user=new User();
        user.setRecommend(recommend);
        user.setCardId(cardId);
        user.setPhone(phone);
        user.setSort(sort);
        user.setUserId(userId);
        user.setUserName(userName);
        int row=userService.updateByPrimaryKeySelective(user);
        if(row>0){
            map.put("result","1");
        }else {
            map.put("result", "0");
        }
        return map;
    }

    @ResponseBody
    @RequestMapping("/user/getUserShow")
    public ModelAndView UserShow(@RequestParam("userId") int userId){
        Config config=configService.selectByPrimaryKey(1);
        ModelAndView modelAndView = new ModelAndView("user/UserShow");
        User user=userService.selectByPrimaryKey(userId);
        modelAndView.addObject("user",user);
        return modelAndView;
    }

    @ResponseBody
    @RequestMapping("/user/JsonUser")
    public String JsonUser(int userId) {
        Config config=configService.selectByPrimaryKey(1);
        try {
            String json = HttpRequest.sendGet(config.getUserUrl() + "/API/user/User", "key=" + config.getUserKey() + "&userId=" + userId);
            JsonRootBean jsonRootBean = JSON.parseObject(json, JsonRootBean.class);
            List<User> userList = jsonRootBean.getData();
            if (userList.size() > 0) {
                User user = userList.get(0);
                return layuiJson.layuiJsonList(user, 1);
            }
        } catch (Exception e) {
            {
            }
        }
        return "{\"code\":1001,\"msg\":\"数据为空\",\"count\":0,\"data\":[]}";
    }

    @ResponseBody
    @RequestMapping("/user/JsonUser1")
    public String JsonUser1(String userName) {
        Config config=configService.selectByPrimaryKey(1);
        try {
            String json = HttpRequest.sendGet(config.getUserUrl() + "/API/user/User1", "key=" + config.getUserKey() + "&userName=" + userName);
            JsonRootBean jsonRootBean = JSON.parseObject(json, JsonRootBean.class);
            List<User> userList = jsonRootBean.getData();
            if (userList.size() > 0) {
                User user = userList.get(0);

                //要去掉的属性
                List<String> par = new ArrayList<>();
                par.add("pwd");
                par.add("payPwd");
                par.add("cardId");
                par.add("registerTime");
                par.add("phone");
                par.add("recommend");
                par.add("sort");
                return layuiJson.layuiJsonList(user, 1,par);
            }
        } catch (Exception e) {
            {
            }
        }
        return "{\"code\":1001,\"msg\":\"数据为空\",\"count\":0,\"data\":[]}";
    }

    @ResponseBody
    @RequestMapping("/user/JsonUser2")
    public String JsonUser2(String userName) {
        Config config=configService.selectByPrimaryKey(1);
        try {
            String json = HttpRequest.sendGet(config.getUserUrl() + "/API/user/User1", "key=" + config.getUserKey() + "&userName=" + userName);
            JsonRootBean jsonRootBean = JSON.parseObject(json, JsonRootBean.class);
            List<User> userList = jsonRootBean.getData();
            if (userList.size() > 0) {
                User user = userList.get(0);
                return layuiJson.layuiJsonList(user, 1,"userId");
            }
        } catch (Exception e) {
            {
            }
        }
        return "";
    }

    //根据Id删除会员等级
    @ResponseBody
    @RequestMapping("/user/delUser")
    public Map<String,Object> delUser(@RequestParam("userId") int userId){
        Config config=configService.selectByPrimaryKey(1);
        Map<String,Object> map=new HashMap<>();
//        int ids =Integer.valueOf(userId);
//        String par = "key="+ config.getUserKey()+"&userId="+ids;
//        String rows= HttpRequest.sendGet(config.getUserUrl() +"/API/user/delUser",par);
       int rows= userService.deleteByPrimaryKey(userId);
        if(rows>0){
            map.put("msg","1");
        }else{
            map.put("msg","0");
        }
        return map;
    }
}